package com.zhangzeqin.code.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 创建一个关于与数据库交互的工具类
 * 能实现数据的查询，插入，修改，删除
 */
public class MysqlUtil {
public static void DQL(String sql,Object ... args) throws ClassNotFoundException, SQLException {
    //注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    //获取连接
    Connection connection= DriverManager.getConnection("jdbc:mysql:///scallop", "root", "123456");
    //创建preparedstatement并传入SQL语句
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    //占位符赋值
    for (int i = 0; i < args.length; i++) {
        preparedStatement.setObject(i+1,args[i]);
    }
    //执行sql
    int row=preparedStatement.executeUpdate();
    preparedStatement.close();
    connection.close();
}
}
